const express = require("express");
const router = express.Router();

// 引用模型
const { User, UserCheckin, UserAddress, Role, Author } = require("../model");

// feed
const userfeed = require("../model/user/feed.json");
const rolefeed = require("../model/role/feed.json");
const authorfeed = require("../model/author/feed.json");
const userAddressfeed = require("../model/userAddress/feed.json");
const userCheckinfeed = require("../model/userCheckin/feed.json");

/* GET users listing. */
// router.get("/", function(req, res, next) {
//   res.send("respond with a resource");
// });

/* User和Role中插入数据 */
router.get("/", function(req, res, next) {
  Promise.all([
    User.create({
      username: "itbilu",
      password: "itbilu.com"
    }),
    Role.create({
      roleName: "管理员"
    }),
    Author.bulkCreate(authorfeed)
    // Author.create({ rightName: "超级管理员" })
  ])
    .then(function(results) {
      let user = results[0],
        role = results[1],
        author = results[2];

      user.setUserRoles(role);
      role.setRoleAuthors(author);
      // user.setUserAuthors(author);
      res.set("Content-Type", "text/html; charset=utf-8");
      res.end(
        "创建成功：" +
          JSON.stringify({
            user: user.dataValues,
            role: role.dataValues,
            author: author
          })
      );
    })
    .catch(next);
});

router.get("/create/newuser", function(res, res, next) {
  User.create({
    username: "zoneques",
    password: "12345678"
  }).then(function(user) {
    user.setUserRoles(1);
    res.set("Content-Type", "text/html; charset=utf-8");
    res.end("创建成功：" + JSON.stringify(user));
  });
});

// 向 UserCheckin 插入数据
router.get("/create/checkin", function(res, res, next) {
  User.create({
    username: "itbilu2",
    password: "itbilu.com"
  })
    .then(function(user) {
      let userCheckin = UserCheckin.build({
        loginIp: "127.0.0.1"
      });
      user.setUserCheckin(userCheckin);

      res.set("Content-Type", "text/html; charset=utf-8");
      res.end("UserCheckin 插入数据成功");
    })
    .catch(next);
});

// N:M 插入数据
router.get("/create/userRoles", function(res, res, next) {
  Promise.all([
    User.create({
      username: "itbilu",
      password: "itbilu.com"
    }),
    Role.create({
      roleName: "管理员"
    })
  ])
    .then(function(results) {
      let user = results[0];
      let role = results[1];
      user.setUserRoles(role);
      // 或
      // role.setUserRoles(user);
      res.set("Content-Type", "text/html; charset=utf-8");
      res.end("userRoles 插入数据成功");
    })
    .catch(next);
});

// 查询User及UserCheckin
router.get("/select/user", function(res, res, next) {
  User.findOne({
    include: [
      {
        model: UserCheckin,
        as: "UserCheckin"
      }
    ]
  })
    .then(function(user) {
      console.log(user);
      res.set("Content-Type", "text/html; charset=utf-8");
      res.end(JSON.stringify(user));
    })
    .catch(next);
});

// 查询User及UserAdress
router.get("/select/userAddress", function(res, res, next) {
  User.findOne()
    .then(function(user) {
      user.getAddress();
      res.set("Content-Type", "text/html; charset=utf-8");
      res.end(JSON.stringify(user));
    })
    .catch(next);
});

// 查询User及UserRoles
router.get("/select/userRoles", function(res, res, next) {
  User.findAll({
    where: {
      id: 2
    },
    attributes: ["id", "username", "active"],
    include: [
      {
        model: Role,
        as: "UserRoles",
        attributes: ["id", "roleName"],
        include: [
          {
            model: Author,
            as: "RoleAuthors",
            attributes: ["id", "authorName"]
          }
        ]
      }
    ]
  })
    .then(function(user) {
      user = user[0];
      user.getUserRoles();
      res.set("Content-Type", "text/html; charset=utf-8");
      res.end(JSON.stringify(user));
    })
    .catch(next);
});

// 更新 userCheckin
router.get("/update/userCheckin", function(res, res, next) {
  User.findOne({
    include: [
      {
        model: UserCheckin,
        as: "UserCheckin"
      }
    ]
  })
    .then(function(user) {
      let userCheckin = UserCheckin.build({
        userId: user.id,
        loginIp: "192.168.0.1"
      });
      user.setUserCheckin(userCheckin);
      res.set("Content-Type", "text/html; charset=utf-8");
      res.end(JSON.stringify(user));
    })
    .catch(next);
});

// 删除 user
router.get("/delete/user", function(res, res, next) {
  User.destroy({
    where: {
      id: 2
    }
  })
    .then(function(result) {
      res.set("Content-Type", "text/html; charset=utf-8");
      res.end("删除完成");
    })
    .catch(next);
  // 使用模型实例删除
  // User.findOne().then(function(user){
  // 	user.destroy();
  // 	res.set('Content-Type', 'text/html; charset=utf-8');
  // 	res.end('删除完成');
  // }).catch(next);
});

// 删除 userCheckin
router.get("/delete/userCheckin", function(res, res, next) {
  User.findOne({
    include: [UserCheckin]
  })
    .then(function(user) {
      user.setUserCheckin(null);
      res.set("Content-Type", "text/html; charset=utf-8");
      res.end(JSON.stringify(user));
    })
    .catch(next);
});

/*  user */

/*  user */

module.exports = router;
